package com.example.binpacking.service.Impl;

import com.example.binpacking.mapper.InterviewerMapper;
import com.example.binpacking.mapper.SysmasterMapper;
import com.example.binpacking.mapper.TeacherMapper;
import com.example.binpacking.model.dto.PasswordRequest;
import com.example.binpacking.service.PasswordService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PasswordServiceImpl implements PasswordService {

    @Autowired
    private SysmasterMapper sysmasterMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private InterviewerMapper interviewerMapper;

    public boolean updatePassword(PasswordRequest request) {
        String loginid = request.getLoginid();
        String originalkey = request.getOriginalkey();
        String newkey = request.getNewkey();
        String usertype = request.getUsertype().toLowerCase();

        // 加密新密码
        String encryptedNewKey = DigestUtils.md5Hex(newkey);

        // 根据用户类型进行密码更新
        switch (usertype) {
            case "s":
            case "g":
                return sysmasterMapper.updatePassword(loginid, originalkey, encryptedNewKey) > 0;
            case "t":
                return teacherMapper.updatePassword(loginid, originalkey, encryptedNewKey) > 0;
            case "f":
                return interviewerMapper.updatePassword(loginid, originalkey, encryptedNewKey) > 0;
            default:
                return false;
        }
    }
}
